require 'observable'
require 'view_globals'

require 'Korundum'
include KDE



class MainWin < MainWindow
	extend ObservableField	
	observable_field :open_selected, :start_selected, :stop_selected, :time_up

  slots   'slotNew()', 'slotOpen()', 'slotSave()', 'slotSaveAs()', 'slotQuit()', 'slotStart()', 'slotStop()'
  slots   'slotTimeUp()'
  
  TREE_WIDTH = 220

 def initialize(*args)
        super

	@projects = {}
	@tasks = {}
	@timer = Qt::Timer.new(self)
	connect(@timer, SIGNAL('timeout()'), self, SLOT('slotTimeUp()'))
	@timer.start(1000)

	ViewGlobals.instance.main_view = self
        init_actions
        init_menus
	init_win
 #       initToolBar
#        initStatusBar

 end
 
 def add_project_data(id, name, description)
	item = Qt::ListViewItem.new(@tree, String.new(name))
	@projects[item] = id
 end

 def add_task_data(id, name, description, project_id)
	proj_item = get_project_item(project_id)
	before = proj_item.first_child
	before = before.next_sibling while before && before.next_sibling

	item = Qt::ListViewItem.new(proj_item, before, String.new(name)) if before
	item = Qt::ListViewItem.new(proj_item, String.new(name)) if !before
	@tasks[item] = id
 end
 
 def clear_tree
	@tree.clear
 end

 def mark_started(task_id)
 end

 def mark_stopped(task_id)
 end

 def update_times(time_data)
	 time_data.each {|task_data| update_task_times(task_data)}
 end

 private

 def init_actions
     # "File" menu items
     @open_action   = StdAction.open(self, SLOT('slotOpen()'), actionCollection)
     @quit_action   = StdAction.quit(self, SLOT('slotQuit()'), actionCollection)
     @start_action  = Action.new('&Start', Shortcut.new, self, SLOT('slotStart()'), actionCollection, 'start_action')
     @stop_action  = Action.new('S&top', Shortcut.new, self, SLOT('slotStop()'), actionCollection, 'stop_action')

 end

 def init_menus
        file_menu = Qt::PopupMenu.new(self)
        task_menu = Qt::PopupMenu.new(self)
        
	@open_action.plug(file_menu)
        file_menu.insertSeparator
        @quit_action.plug(file_menu)
	
	@start_action.plug(task_menu)
	@stop_action.plug(task_menu) 

        menuBar.insertItem("&File", file_menu)
        menuBar.insertItem("&Task", task_menu)

	helpMenu = helpMenu("")
        menu_bar().insert_item("&Help", helpMenu)
 end

 def init_win
   mainGeom  = Qt::Rect.new(0, 0, 640, 500)
   setGeometry(mainGeom)
   @mainView  = Qt::Splitter.new(self, "main view")
   @tree      = KDE::ListView.new(@mainView, "tree")
   setCentralWidget(@mainView)
   init_list_view
 end

 def init_list_view
	 @tree.sorting = -1
	 @tree.addColumn("Project/Task", TREE_WIDTH - 21)
	 @tree.addColumn("Today")
	 @tree.addColumn("Total")
         @mainView.setSizes([TREE_WIDTH, 375])
         @tree.setRootIsDecorated(true)
 end

 def get_task_item(task_id)
	 @tasks.invert[task_id]
 end

 def get_project_item(project_id)
	 @projects.invert[project_id]
 end

 def slotOpen
	open_selected.notify(self)
 end

 def slotStart
	task_id = get_selected_task
	start_selected.notify(task_id) if task_id
 end
 
 def slotStop
	task_id = get_selected_task
	stop_selected.notify(task_id) if task_id
 end

 def slotTimeUp
	 time_up.notify
 end

 def get_selected_task
	return nil if !@tree.selected_item
	@tasks[@tree.selected_item]
 end
 
 def update_task_times(task_data)
	id = task_data[0]
	today = task_data[1]
	total = task_data[2]
	item = get_task_item(id)
	item.set_text(1, today.to_s)
	item.set_text(2, total.to_s)
 end
end
